﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfumeShopManagement.Controller;
using PerfumeShopManagement.Model;
using PerfumeShopManagement.Utility;

namespace PerfumeShopManagement.View
{
    public partial class frmCustomerManagement : Form
    {
        public frmCustomerManagement()
        {
            InitializeComponent();
        }

        private void resetFieldWithoutId()
        {
            // Reset without id
            txbCustomerName.Text = "";
            txbPhoneNumber.Text = "";
            txbAddress.Text = "";
            cbbAccount.Text = "";
        }

        private void setNextId()
        {
            txbCustomerId.Text = Constants.AUTO_NUMBERING_MESSAGE;
        }

        private void reloadUserList()
        {
            User u = new User();
            u.RoleId = Constants.USER_ROLE;
            List<object> users = ControllerFactory.AccountController.GetObjectsByCriteria(u);
            cbbAccount.Items.Clear();
            foreach (User user in users)
            {
                cbbAccount.Items.Add(user.Username);
            }
        }

        private void reloadGridView()
        {
            // Load grid view
            object[] buffer = new object[5];
            List<DataGridViewRow> rows = new List<DataGridViewRow>();
            List<object> customers = ControllerFactory.CustomerController.GetAllObjects();
            foreach (Customer item in customers)
            {
                buffer[0] = item.CustomerId;
                buffer[1] = item.CustomerName;
                buffer[2] = item.PhoneNumber;
                buffer[3] = item.Address;
                buffer[4] = item.Username;

                rows.Add(new DataGridViewRow());
                rows[rows.Count - 1].CreateCells(gridCustomers, buffer);
            }
            gridCustomers.Rows.Clear();
            gridCustomers.Rows.AddRange(rows.ToArray());
        }

        private void btnAddCustomer_Click(object sender, EventArgs e)
        {
            setNextId();
            resetFieldWithoutId();
        }

        private void btnDeleteCustomer_Click(object sender, EventArgs e)
        {
            if (gridCustomers.SelectedRows.Count > 0)
            {
                bool allDeleted = true;
                foreach (DataGridViewRow item in gridCustomers.SelectedRows)
                {
                    int id = (int)item.Cells["colCustomerId"].Value;
                    if (!ControllerFactory.CustomerController.DeleteById(id))
                        allDeleted = false;
                }

                if (allDeleted)
                {
                    reloadGridView();
                    MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
                }
                else
                {
                    MessageBox.Show(Constants.FAILED_MESSAGE);
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            resetFieldWithoutId();
        }

        private void btnSaveCustomer_Click(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            if (NumberUtil.IsNumeric(txbCustomerId.Text))
                customer.CustomerId = int.Parse(txbCustomerId.Text);
            customer.CustomerName = txbCustomerName.Text;
            customer.PhoneNumber = txbPhoneNumber.Text;
            customer.Address = txbAddress.Text;

            if (ControllerFactory.CustomerController.Save(customer))
            {
                reloadGridView();
                MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
            }
            else
            {
                MessageBox.Show(Constants.FAILED_MESSAGE);
            }
        }

        private void frmCustomerManagement_Load(object sender, EventArgs e)
        {
            setNextId();
            resetFieldWithoutId();
            reloadUserList();
            reloadGridView();
        }

        private void btnAddNewAccount_Click(object sender, EventArgs e)
        {
            frmAccountManagement accountManagementView = new frmAccountManagement();
            accountManagementView.Visible = true;
            accountManagementView.FormClosed += new FormClosedEventHandler(accountManagementView_FormClosed);
            this.Enabled = false;
        }

        void accountManagementView_FormClosed(object sender, FormClosedEventArgs e)
        {
            reloadUserList();
            this.Enabled = true;
        }

        private void btnEditCustomer_Click(object sender, EventArgs e)
        {
            if (gridCustomers.SelectedRows.Count > 0)
            {
                DataGridViewRow item = gridCustomers.SelectedRows[0];
                txbCustomerId.Text = ((int)item.Cells["colCustomerId"].Value).ToString();
                txbCustomerName.Text = (string)item.Cells["colCustomerName"].Value;
                txbPhoneNumber.Text = (string)item.Cells["colPhoneNumber"].Value;
                txbAddress.Text = (string)item.Cells["colAddress"].Value;
                cbbAccount.SelectedValue = (string)item.Cells["colAccountName"].Value;
            }
        }
    }
}
